﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Net;
using System.Web;

    public class KeepAlive
    {
        private static KeepAlive instance;
        private static object sync = new object();
        private string _applicationUrl;
        private string _cacheKey;

        private KeepAlive(string applicationUrl)
        {
            _applicationUrl = applicationUrl;
            _cacheKey = Guid.NewGuid().ToString();
            instance = this;
        }

        public static bool IsKeepingAlive
        {
            get
            {
                lock (sync)
                {
                    return instance != null;
                }
            }
        }

        public static void Start(string applicationUrl)
        {
            if (IsKeepingAlive)
            {
                return;
            }
            lock (sync)
            {
                instance = new KeepAlive(applicationUrl);
                instance.Insert();
            }
        }

        public static void Stop()
        {
            lock (sync)
            {
                HttpRuntime.Cache.Remove(instance._cacheKey);
                instance = null;
            }
        }

        private void Callback(string key, object value, CacheItemRemovedReason reason)
        {
            if (reason == CacheItemRemovedReason.Expired)
            {
                FetchApplicationUrl();
                Insert();
            }
        }

        private void Insert()
        {
            HttpRuntime.Cache.Add(_cacheKey,
                this,
                null,
                Cache.NoAbsoluteExpiration,
                new TimeSpan(0, 10, 0),
                CacheItemPriority.Normal,
                this.Callback);
        }

        private void FetchApplicationUrl()
        {
            try
            {
                HttpWebRequest request = HttpWebRequest.Create(this._applicationUrl) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    HttpStatusCode status = response.StatusCode;
                    //log status
                }
            }
            catch
            {
                //log exception
            }
        }
    }


